package com.app.studyroom.mapper;

import com.app.studyroom.db.AreaInfo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AreaNameToIdMapper {

    private static final Map<String, String> nameToId = new HashMap<>();
    private static final Map<String, String> idToName = new HashMap<>();

    static {
        // 静态默认映射（保证旧代码兼容）
        nameToId.put("B校区01自习室", "201");
        nameToId.put("B校区02自习室", "202");

        for (Map.Entry<String, String> entry : nameToId.entrySet()) {
            idToName.put(entry.getValue(), entry.getKey());
        }
    }

    /**
     * ✅ App 启动后通过接口更新映射
     */
    public static void updateMappingFromApi(List<AreaInfo> areaList) {
        nameToId.clear();
        idToName.clear();

        for (AreaInfo area : areaList) {
            nameToId.put(area.qyName, String.valueOf(area.id));
            idToName.put(String.valueOf(area.id), area.qyName);
        }
    }

    public static String getIdByName(String name) {
        return nameToId.get(name);
    }

    public static String getNameById(String id) {
        return idToName.get(id);
    }

    public static String[] getAllAreaNames() {
        return nameToId.keySet().toArray(new String[0]);
    }
}
